package com.yujistu.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.yujistu.R;
import com.yujistu.fragment.appointfragment.AppointFragment;
import com.yujistu.fragment.appointfragment.CourseFragment;
import com.yujistu.fragment.HomeFragment;
import com.yujistu.fragment.mefragment.MeFragment;
import com.yujistu.fragment.PlanFragment;

public class MainActivity extends BaseActivity {

    //定义FragmentTabHost对象
     FragmentTabHost   fragmentTabHost;

    //定义一个布局
    LayoutInflater   layoutInflater;
    //定义数组来存放Fragment界面
    private Class fragmentArray[]={HomeFragment.class, PlanFragment.class, AppointFragment.class, CourseFragment.class, MeFragment.class};

    //定义数组来存放按钮图片
    private int  mImageViewArray[]={R.drawable.selector_home_radio_button, R.drawable.selector_plan_radio_button, R.drawable.selector_appoint_radio_button, R.drawable.selector_course_radio_button, R.drawable.selector_me_radio_button};

    //Tab选项卡的文字
    private String mTextviewArray[]={"首页","计划","预约","课程","我的"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    /**
     * 初始化组件
     */
    private void initView() {
        //实例化布局对象
        layoutInflater = LayoutInflater.from(this);

        //实例化TabHost对象，得到TabHost
        fragmentTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
        fragmentTabHost.setup(this, getSupportFragmentManager(), R.id.real_content);

        //得到fragment的个数
        int count = fragmentArray.length;

        for(int i = 0; i<count; i++){
            //为每一个Tab按钮设置图标、文字和内容
            TabHost.TabSpec  tabSpec = fragmentTabHost.newTabSpec(mTextviewArray[i]).setIndicator(getTabItemView(i));
            //将Tab按钮添加进Tab选项卡中
            fragmentTabHost.addTab(tabSpec, fragmentArray[i], null);
            //设置Tab按钮的背景
           // fragmentTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.selector_home_radio_button);
        }
    }

    /**
     * 给Tab按钮设置图标和文字
     */
    private View getTabItemView(int index) {
        View view = layoutInflater.inflate(R.layout.view_tab_item, null);

        ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
        imageView.setImageResource(mImageViewArray[index]);

        TextView  textView = (TextView)view.findViewById(R.id.textview);
        textView.setText(mTextviewArray[index]);

        return view;
    }
}
